<template>
  <el-form v-loading="loading" :inline="true" ref="ruleFormRef" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="订单类别:" prop="OrderType">
      <el-input disabled v-model="form.OrderType" />
    </el-form-item>
    <el-form-item label="订单状态:" prop="orderStatus">
      <el-select disabled v-model="form.orderStatus"  placeholder="请选择订单状态">
        <el-option  v-for="item in orderstatus" :key="item.id" :label="item.dictLabel" :value="item.dictValue" />
      </el-select>
    </el-form-item>
    <el-form-item label="订单管理员:" prop="orderManager">
      <el-tree-select  v-model="form.orderManager" :data="data" :render-after-expand="false" placeholder="请选择客户经理" />
    </el-form-item>
    <el-form-item label="客户姓名:" prop="clientName">
      <el-input v-model="form.clientName" />
    </el-form-item>
    <el-form-item label="客户电话:" prop="phone">
      <el-input v-model="form.phone" />
    </el-form-item>
    <el-form-item label="地址:" prop="address">
      <el-input v-model="form.address" />
    </el-form-item>
    <el-form-item label="合作方:" prop="partner">
      <el-input v-model="form.partner" />
    </el-form-item>
   
    
    <el-form-item label="定金金额:" >
      <el-input v-model="form.reserveAmount" />
    </el-form-item>
    <el-form-item label="合同金额:" >
      <el-input v-model="form.amount" />
    </el-form-item>
    <el-form-item label="备注:">
      <el-input v-model="form.comment" type="textarea" style="width: 400px" />
    </el-form-item>
  </el-form>
  <div class="_button">
    <el-button type="primary" size="large" @click="onSubmit(ruleFormRef)">保存</el-button>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive,defineEmits  } from 'vue'
import {  tbOrderSave} from '/@/api/tb/tbOrder'
import { ElMessage } from 'element-plus'
import {GetWhole} from "../Util_Function/getOrderWhole"
import { tbDictList } from '/@/api/tb/tbDictType'
import {useUserStore} from "../../../store/modules/user"
const data = ref([])
const orderstatus = ref([])
const ruleFormRef = ref<any>()
const loading=ref(true)


const emit = defineEmits(['editForm'])
const form = reactive({
  OrderType:'',//订单类别
  createTime: '', // 订单创建时间
  clientName: '', // 客户姓名
  phone: '', // 联系电话
  address: '', // 地址
  partner: '', // 合作方
  orderManager: '', // 订单管理员
  orderStatus: '1', // 订单状态
  reserveAmount: '', // 定金金额
  amount: '', // 合同金额
  comment: '' // 备注
})
const userstore=useUserStore().userInfo
console.log(userstore);

const getOffices = async () => {
  loading.value=true
  for (let key in form) {
        form[key]=''
  }
  form.OrderType='自然订单'
  form.orderStatus='1'
  const status = await tbDictList({ dict: 'the_order_status' })
  orderstatus.value = status.data
  console.log(status.data);
  
  data.value = await GetWhole();
  if(data.value.length&&orderstatus.value.length){
    loading.value=false
  }
}
getOffices()

// const GetUserRole = async () => {
//     for(let i=0;i<userstore?.roleList?.length;i++){
//        if(userstore?.roleList[i].roleName=="销售主管"||"销售人员"){
//         isshowNature.value=true;
//        }
//     }
//   };
//   GetUserRole();
const onSubmit = async (formEl) => {
  if (!formEl) return
  await formEl.validate( async(valid) => {
    if (valid) {
      const res= await tbOrderSave(form)
  if(res.result){
    for (let key in form) {
        form[key]=''
        form.afterSales='0'
        form.orderStatus='1'
  }
    emit('editForm') 
    ElMessage({
    message:res.message,
    type: 'success',
  })
  }else{
    ElMessage.error(res.message)
  }} 
  })
}
const rules = reactive({
  OrderType: [{ required: true,message: '请输入客户姓名',trigger: 'blur'}],
  clientName: [{ required: true,message: '请输入客户姓名',trigger: 'blur'}],
  phone: [{ required: true,message: '请输入联系电话',trigger: 'blur'}],
  address: [{ required: true,message: '请输入地址',trigger: 'blur'}],
  partner: [{ required: true,message: '请输入合作方',trigger: 'blur'}],
  orderManager: [{ required: true,message: '请选择客户经理',trigger: 'change'}],
  orderStatus: [{ required: true,message: '请选择订单状态',trigger: 'change'}],
})

</script>
<style lang="less" scoped>
._button {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  padding-right: 50px;
}
.el-input {
  width: 300px;
}
</style>
